*************************************************************************
*																		*
*					Campaign Panel Book Chapter 6 - Choice variable		*
*																		*
*************************************************************************	

*******************************************************************************
* Generate an alternative operationalization, other parties in reference category
*******************************************************************************
** I.1 Generate vote intention variable
*******************************************************************************   

//Pre-election vote intention
foreach welle of numlist 1/6 {
   recode kp`welle'_190b (993 997 999 1000=.) (801=10), copyrest gen(wabsa`welle')
}

// ( 10 = other party)

tab kp1_190b wabsa1, m

// Post-election voting behaviour
recode kp7_200b (801=10) (993 994 997 998 999 1000=.), ///
   copyrest gen(wabsa7)
  
//2009 problem: no dependent interviews, first interview as postal voter counts
foreach welle of numlist 3/7 {
replace wabsa`welle'= kp3_191b if kp3_170==6
recode wabsa`welle' (801=10) (997 998 999 1000=.)  if kp3_170==6
}

foreach welle of numlist 4/7 {
replace wabsa`welle' = kp4_191b if kp4_170==6 & kp3_170~=6 & kp4_191b~=93
recode wabsa`welle' (801=10) (997 998 999 1000=.) if kp4_170==6 & kp3_170~=6 & kp4_191b~=93
}

foreach welle of numlist 5/7 {
replace wabsa`welle' = kp5_191b if kp5_170==6 & kp4_170~=6 & kp3_170~=6 & kp5_191b~=.
recode wabsa`welle' (801=10) (997 998 999 1000=.) if kp5_170==6 & kp4_170~=6 & kp3_170~=6 & kp5_191b~=.
}

foreach welle of numlist 6/7 {
replace wabsa`welle' = kp6_191b if kp6_170==6 & kp5_170~=6 & kp4_170~=6 & kp3_170~=6 & kp6_191b~=.
recode wabsa`welle' (801=10) (997 998 999 1000=.) if kp6_170==6 & kp5_170~=6 & kp4_170~=6 & kp3_170~=6 & kp6_191b~=.
}
  
 fre wabsa? if kp6_170==6 
   
   
   

*******************************************************************************
* I.2 Create binary vote intention dummies (undecided voters are "missings")
*******************************************************************************

foreach welle of numlist 1/7 {
   foreach partei of numlist 1 4/7 {
      gen parteia`partei'`welle' = wabsa`welle'==`partei' if wabsa`welle'!=998 & ///
	     wabsa`welle'!=.
   }
}  

*******************************************************************************
** I.3 Create binary vote intention dummies (undecided voters are taken into account
* as a separate category)
*******************************************************************************

foreach welle of numlist 1/7 {
   foreach partei of numlist 1 4/7 {
      gen parta`partei'`welle' = wabsa`welle'==`partei' if  ///
	     wabsa`welle'!=.
   }
}  //Respondent who did not want to vote in one wave counts as "missing" in this wave

	
list wabsa4 parta14

// Part0 is a dummy indicating indecision
foreach welle of numlist 1/7 {
   gen parta0`welle' = wabsa`welle'==998 if wabsa`welle'!=.
}

tab1 parteia55 parta44

sum parta*




*******************************************************************************
** I.4 Generate variable which indicates whether a repondent possesses a stable
** voting intention thoughout the campaing panel
*******************************************************************************

egen sdwabsa = rowsd(wabsa?)
gen stablea = 0 if sdwabsa!=.
replace stablea = 1 if sdwabsa==0

fre stablea  //52% are stable
fre stablea if w1==0 //W2-starters are included

//list wabsa? if stablea==. //Missing when no valid observation or only one 




**************************************************************************
** Generate variable indicating whether a person has a stable voting	**
** intention for a specific party   									**
**************************************************************************

/* Background: We analyze party vote intention separately for different parties
over time. It is possible that a respondent is unstable over time regarding a 
particular party (e.g., switching between SPD and Greens) but stable regarding other
parties (e.g., will never vote CDU or FDP). Only those respondents are included 
in the party models which show variation regarding a particular party.*/

egen sdwabscdu = rowsd(parta1?)
egen sdwabsspd = rowsd(parta4?)
egen sdwabsfdp = rowsd(parta5?)
egen sdwabsgru = rowsd(parta6?)
egen sdwabslink = rowsd(parta7?)

gen stablecdu = 0 if sdwabscdu!=.
replace stablecdu = 1 if sdwabscdu==0

gen stablespd = 0 if sdwabsspd!=.
replace stablespd = 1 if sdwabsspd==0

gen stablefdp = 0 if sdwabsfdp!=.
replace stablefdp = 1 if sdwabsfdp==0

gen stablegru = 0 if sdwabsgru!=.
replace stablegru = 1 if sdwabsgru==0

gen stablelink = 0 if sdwabslink!=.
replace stablelink = 1 if sdwabslink==0

fre stable*  //3781 cases

lab define stable ///
	0 "Variation" ///
	1 "immer stabil"
lab val stable* stable

/* Among parties the share of stable voters is less volatile than in 2013,
roughly 80% are always stable */


*----------------------------------------
* Never ever Non-vote
*----------------------------------------
/* Only those respondents are included in the party vote models who are constant
in their desire to turn out; this is to ensure that mobilization effects and 
directional effects are not interchanged.*/


* Dummies
foreach welle of numlist 1/6 {
	gen wbtdummy`welle' = 1 if kp`welle'_170<=3 | kp`welle'_170==6
	replace wbtdummy`welle' = 0 if (kp`welle'_170>3 ///
		& kp`welle'_170<=5) | kp`welle'_170==98
	lab var wbtdummy`welle' "Wahlbeteiligung Welle `welle'"
}



recode kp7_180 (2=0) (99=.), gen(wbtdummy7)
lab var wbtdummy7 "Wahlbeteiligung Welle 7"

// Integrate postal voters, first disclosure postal vote counts
foreach welle of numlist 3/7 {
replace wbtdummy`welle'= 1 if kp3_170==6
}

foreach welle of numlist 4/7 {
replace wbtdummy`welle' = 1 if kp4_170==6 & kp3_170~=6
}

foreach welle of numlist 5/7 {
replace wbtdummy`welle' = 1 if kp5_170==6 & kp4_170~=6 & kp3_170~=6
}

foreach welle of numlist 6/7 {
replace wbtdummy`welle' = 1 if kp6_170==6 & kp5_170~=6 & kp4_170~=6 & kp3_170~=6
}

* Target variable
gen neverno = 1 
replace neverno = 0 if wbtdummy1==0 | wbtdummy2==0 | ///
	wbtdummy3==0 | wbtdummy4==0 | ///
	wbtdummy5==0 | wbtdummy6==0 | ///
	wbtdummy7==0 //minimum once non-vote


lab define neverno ///
	0 "phasenweise Nichtwähler" ///
	1 "niemals Nichtwahlabsichten"
lab val neverno neverno
fre neverno //17% indicate not wanting to turn out at least once


*---------------------------------------------------------
* How many valid observations exist for the DV?
*------------------------------------------------------------


* 7 valid observations in vote intention?
gen allvalid = 0
replace allvalid = 1 if !missing(wabsa1, wabsa2, wabsa3, wabsa4, ///
	wabsa5, wabsa5, wabsa6, wabsa7)

fre allvalid  //only 27% (so low due to W2-starters)
fre allvalid if kp4_170==6

list wabsa? allvalid

gen include = 0
replace include = 1 if teilnahme==7 | (teilnahme==6 & w1==0)
lab var include "Teilnahme an allen Wellen (7 bzw. 6)"

*------------------------------------------------------
* Number of cases for party models
*------------------------------------------------------

cd "${path}\Reproducible Do-Files\Chapter 6"

estimates clear
foreach var of varlist stable* {
eststo: estpost tab `var' 
}

esttab using Fallauswahl.rtf, cells ("b pct(fmt(0))") noobs ///
	title("Fallauswahl: Mind. zwei gültige Beobachtungen Wahlabsicht." ///
	"Befragte mit stabilen und nichtstabilen Wahlabsichten") ///
	mtitle("Allgemein" "CDU/CSU" "SPD" "FDP" "Grüne" "Linke") compress	///
	replace
	
estimates clear	
foreach var of varlist stable* {
eststo: estpost tab `var' if neverno==1
}

esttab using Fallauswahl.rtf, cells ("b pct(fmt(0))") noobs ///
	title("Fallauswahl: Mind. zwei gültige Beobachtungen Wahlabsicht + zu keinem Zeitpunkt Nichtwahlabsicht." ///
	"Befragte mit stabilen und nichtstabilen Wahlabsichten") ///
	mtitle("Allgemein" "CDU/CSU" "SPD" "FDP" "Grüne" "Linke") compress	///
	append

estimates clear	
foreach var of varlist stable* {
eststo: estpost tab `var' if include==1
}

esttab using Fallauswahl.rtf, cells ("b pct(fmt(0))") noobs ///
	title("Fallauswahl: Teilnahme an allen 7 Wellen." ///
	"Befragte mit stabilen und nichtstabilen Wahlabsichten") ///
	mtitle("Allgemein" "CDU/CSU" "SPD" "FDP" "Grüne" "Linke") compress	///
	append

estimates clear
foreach var of varlist stable* {
eststo: estpost tab `var' if neverno==1 & include==1
}

esttab using Fallauswahl.rtf, cells ("b pct(fmt(0))") noobs ///
	title("Fallauswahl: Teilnahme an allen 7 Wellen + zu keinem Zeitpunkt Nichtwähler." ///
	"Befragte mit stabilen und nichtstabilen Wahlabsichten") ///
	mtitle("Allgemein" "CDU/CSU" "SPD" "FDP" "Grüne" "Linke") compress	///
	append


estimates clear
foreach var of varlist stable* {
eststo: estpost tab `var' if allvalid==1
}

esttab using Fallauswahl.rtf, cells ("b pct(fmt(0))") noobs ///
	title("Fallauswahl: 7 gültige Beobachtungen Wahlabsicht." ///
	"Befragte mit stabilen und nichtstabilen Wahlabsichten") ///
	mtitle("Allgemein" "CDU/CSU" "SPD" "FDP" "Grüne" "Linke") compress	///
	append



fre stable* if neverno==1 & allvalid==1


* How many votes does the SPD receive?
egen countspd = rowtotal(parta4?), missing

* For instable voters: in precisely which waves is the vote intention for a 
* particular party observed?

egen spdpattern = concat(parta4?) if allvalid==1 

estimates clear
foreach num of numlist 0/7 {
estpost tab spdpattern if countspd==`num' & allvalid==1
estimates store num`num'
}

foreach num of numlist 0/7 {
esttab num`num' using "Pattern SPD-Wahlabsichten.rtf", cells("b pct(fmt(0))") noobs ///
	title("`num' mal Absicht für SPD:") ///
	nonumber append
}



*------------------------------------------------------------
* Lazarsfeld-Types without non-voters but with W2-starters
*-----------------------------------------------------------

* this code has to be adjusted according to the existing data structure

* Transform variable vote intention into correct format

* Recode necessary in order to be able to copy code from Do-File: "Wahlabsicht Kreuzungen"
* other parties besides the five established ones should have the same code
* "do not know" = 888
* Non-voters are already "missing", postal voters are integrated

foreach num of numlist 1/7 {
gen wabsrestr_novote`num' = wabsa`num'
recode wabsrestr_novote`num' (8/10=801) (998=888)
}

gen wabsrestr_novote1_w2 = wabsrestr_novote1  	
replace wabsrestr_novote1_w2 = wabsrestr_novote2 if w1==0		//für W2-Starter W1 mit W2 auffüllen


tab wabsa4 wabsrestr_novote4, m 

* target manifestations
label define lazar3 ///
	1 "Partisan" ///
	2 "Crystalizer" ///
	3 "Waverer" ///
	4 "Party Changer"
	

/* !!!!!!!!!!!!!!! With W2-starters   !!!!!!!!!!!!!!!!! */

gen lazarstyp_novote_cry_w2=.

*** Partisans (for all parties, including stable others)
replace lazarstyp_novote_cry_w2=1 if wabsrestr_novote1_w2==wabsrestr_novote2 & wabsrestr_novote2==wabsrestr_novote3 & ///
	wabsrestr_novote3==wabsrestr_novote4 & wabsrestr_novote4==wabsrestr_novote5 & wabsrestr_novote5==wabsrestr_novote6  ///
	& wabsrestr_novote6==wabsrestr_novote7 & wabsrestr_novote7<=801


*** Crystalizer (A vote intention can only crystallize for undecided voters. This vote intention does not necessarily have to remain stable)
replace lazarstyp_novote_cry_w2=2 if wabsrestr_novote1_w2==888 & wabsrestr_novote2==888 & ///
	wabsrestr_novote3==888 & wabsrestr_novote4==888 & wabsrestr_novote5==888  & wabsrestr_novote6==888 & wabsrestr_novote7<888
replace lazarstyp_novote_cry_w2=2 if wabsrestr_novote1_w2==888 & wabsrestr_novote2==888 & ///
	wabsrestr_novote3==888 & wabsrestr_novote4==888 & wabsrestr_novote5==888  & wabsrestr_novote6<888 & wabsrestr_novote7<888
replace lazarstyp_novote_cry_w2=2 if wabsrestr_novote1_w2==888 & wabsrestr_novote2==888 & ///
	wabsrestr_novote3==888 & wabsrestr_novote4==888 & wabsrestr_novote5<888 & wabsrestr_novote6<888 ///
	& wabsrestr_novote7<888
replace lazarstyp_novote_cry_w2=2 if wabsrestr_novote1_w2==888 & wabsrestr_novote2==888 & ///
	wabsrestr_novote3==888 & wabsrestr_novote4<888  & wabsrestr_novote5<888 & wabsrestr_novote6<888 ///
	& wabsrestr_novote7<888
replace lazarstyp_novote_cry_w2=2 if wabsrestr_novote1_w2==888 & wabsrestr_novote2==888 & ///
	wabsrestr_novote3<888 & wabsrestr_novote4<888  & wabsrestr_novote5<888 & wabsrestr_novote6<888  ///
	& wabsrestr_novote7<888
replace lazarstyp_novote_cry_w2=2 if wabsrestr_novote1_w2==888 & ///
	wabsrestr_novote2<888 & wabsrestr_novote3<888 & wabsrestr_novote4<888 ///
	& wabsrestr_novote5<888 & wabsrestr_novote6<888   & wabsrestr_novote7<888

***Waverer (Respondents indicate the same party in wave 1 and wave 7 but make different statements in some intermediate waves)
replace lazarstyp_novote_cry_w2=3 if wabsrestr_novote1_w2==wabsrestr_novote7 & (wabsrestr_novote1_w2!=wabsrestr_novote2 | ///
	wabsrestr_novote1_w2!=wabsrestr_novote3 | wabsrestr_novote1_w2!=wabsrestr_novote4 | wabsrestr_novote1_w2!=wabsrestr_novote5 ///
	| wabsrestr_novote1_w2!=wabsrestr_novote6) & wabsrestr_novote7<888

***Party Changer (Respondents who change their vote intention between wave 1 and wave 7)
replace lazarstyp_novote_cry_w2=4 if wabsrestr_novote1_w2<888 & wabsrestr_novote7<888 & wabsrestr_novote1_w2!=wabsrestr_novote7

***Unsure Crystalizer (also coded as Crystalizer)
replace lazarstyp_novote_cry_w2=2 if lazarstyp_novote_cry_w2==. & !missing(wabsrestr_novote1_w2, wabsrestr_novote2, ///
	wabsrestr_novote3, wabsrestr_novote4, wabsrestr_novote6, wabsrestr_novote7)

replace lazarstyp_novote_cry_w2=. if missing(wabsrestr_novote1_w2, wabsrestr_novote2, ///
	wabsrestr_novote3, wabsrestr_novote4, wabsrestr_novote5, wabsrestr_novote6, wabsrestr_novote7)


label value lazarstyp_novote_cry_w2 lazar3

list wabsrestr_novote1_w2 wabsrestr_novote2-wabsrestr_novote7 laza if laza==2


fre laza if wabsrestr_novote7==.



*-------------------------------------
* Save 
*--------------------------------------

keep lfdn wabsa? parta?? stable* neverno allvalid include laza

save "${path}\Reproducible Do-Files\Chapter 6\Data\choice09.dta", replace

